package com.ujs.controller;
import com.ujs.pojo.Order;
import com.ujs.pojo.Product;
import com.ujs.service.OrderService;
import com.ujs.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;





    @RequestMapping("/findAll")
    public String findAllByPage(Model model,@RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
    @RequestParam(name = "pageSize", required = true, defaultValue = "4") Integer pageSize){
        List<Order> list = orderService.findAll();
        PageUtil<Order> pageInfo = new PageUtil<>(page, pageSize, list);
        List<Order> orders = pageInfo.getList();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("orders", orders);
        return "orders-list";
    }

    @GetMapping("/findByOrderNumLike")
    public String findByOrderNumLike(Model model,String num,
                                        @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                                        @RequestParam(name = "pageSize", required = true, defaultValue = "4") Integer pageSize){
        List<Order> list = orderService.findByOrderNumLike(num);
        PageUtil<Order> pageInfo = new PageUtil<>(page, pageSize, list);
        List<Order> orders = pageInfo.getList();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("orders", orders);
        return "orders-list";
    }



    @GetMapping("/orders-add")
    public String add(Model model ){/*, @RequestParam Product product)*/
        return "orders-add";
    }
    @GetMapping("/deleteById")
    public String deleteById(Model model,String id ){
        System.out.println(id);
        orderService.deleteById(id);
        return "orders-list";
    }



    @RequestMapping("/save")
    public String save(Model model,  Order order) {
        System.out.println("收到的数据为："+order);
        orderService.save(order);
        return "redirect:findAll";
    }
    @RequestMapping("/toOrderShow")
    public String findById(Model model,  String id) {
        Order order = orderService.findById(id);

        model.addAttribute("order",order);
        return "orders-show";
    }
    @RequestMapping("/orders-show")
    public String orderShow(){
        return "orders-show";
    }
}


